<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px">
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
            <el-form-item label="商品SKU" prop="sku">
                <el-input v-model.trim="queryParams.spu" placeholder="请输入商品SKU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="全部" clearable  style="width: 240px">
                    <el-option v-for="(dict, index) in PROCURE_STATUS_LIST" :key="index" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableData" border script>
            <el-table-column label="ID" prop="id" min-width="30"></el-table-column>
            <el-table-column label="商品名称" prop="product_name" min-width="160"></el-table-column>
            <el-table-column label="商品SPU" prop="spu" min-width="60"></el-table-column>
            <el-table-column label="颜色" prop="color" min-width="60"></el-table-column>
            <el-table-column label="尺码" prop="size" min-width="60"></el-table-column>
            <el-table-column label="sku采购数量" prop="purchase_nums" min-width="80" show-overflow-tooltip></el-table-column>
            <el-table-column label="SKU" prop="seller_sku" min-width="160"></el-table-column>
            <el-table-column label="状态" prop="status" min-width="100">
                <template slot-scope="scope">
                    {{ scope.row.status | formatLabelByValueFuzzy(PROCURE_STATUS_LIST) }}
                </template>
            </el-table-column>
            <el-table-column label="操作" min-width="100">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-printer" @click="print_this_code(scope.row)">{{ scope.row.status == 2 ? '已打印' : '打印' }}</el-button>
                    <el-button type="text" icon="el-icon-edit" @click="ResetStatus(scope.row)">重置</el-button>
                </template>
            </el-table-column>
            
            <!-- <el-table-column label="产品图片" min-width="120">
                <template slot-scope="scope">
                    <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                </template>
            </el-table-column> -->
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
    </el-dialog>
</template>

<script>
import {getLodop} from '@/utils/LodopFuncs';
import { exportFile } from '@/utils/index'
import { querySupplierProcurementInfo, changeSupplierProcurementStatus,resertSupplierProcurementStatus } from '@/api/supplierProcurement'
import { PROCURE_STATUS_LIST } from '../constant'
export default {
    name: 'ProductOutSpuListDialog',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            queryParams: {
                spu: undefined,
                sku: undefined,
                contract_number: undefined,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            total_new_quantity: 0,
            PROCURE_STATUS_LIST,
            input: 5,
        }
    },
    computed: {
        title() {
            return `SPU详情`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    async mounted() {
        this.loading = true
        try {
            // const warehouseData = await getWarehouse()
            // this.dict.warehouseList = warehouseData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.queryParams.contract_number = this.params.contract_number
        this.getList()
    },
    methods: {
        ResetStatus(row){
            this.$confirm(`是否确认重置?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                resertSupplierProcurementStatus({ id: row.id,resert_status: 1 }).then(res=>{
                    this.$message.success(res.msg)
                    this.$emit('success')
                }).finally(()=>{
                    this.pageInfo.page = 1
                    this.getList()
                })
            }).catch(() => { })
        },
        print_this_code(row){
            if(row.status == 2) {
                return this.$message.warning('无需再次打印!')
            }
            this.$confirm('执行操作会产生打印，确定继续吗？', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
            .then(() => {
                let LODOP = getLodop();
                
                console.log(LODOP);
                
                if(LODOP) {
                    LODOP.PRINT_INIT("入库码第1批");
                    LODOP.SET_PRINT_PAGESIZE(1,'40mm','20mm','');
                    LODOP.SET_PRINT_MODE("POS_BASEON_PAPER",true);

                    LODOP.NewPage();
                    LODOP.ADD_PRINT_BARCODE('1mm','1mm','18mm','18mm',"QRCode", row.seller_sku);
                    LODOP.ADD_PRINT_TEXT('1mm','15mm','30mm','1mm',row.seller_sku);
                    const bin = [row.spu, row.color, row.size]
                    bin.forEach((value, n ) => {
                          const num_n = 2+(n+1)*3 +'mm';
                          LODOP.ADD_PRINT_TEXT(num_n,'15mm','22mm','1mm',value);
                      })
                    LODOP.SET_PRINT_MODE("CUSTOM_TASK_NAME","入库码第1批");
                    // 设置打印成功的回调函数
                    LODOP.On_Return = (TaskID, Value) => {
                        if (Value) {
                            changeSupplierProcurementStatus({id: row.id, status: 2}).then(res => {
                            this.$message.success('打印成功')
                            this.getList()
                        })
                        } else {
                            this.$message.error('打印失败')
                        }
                    };
                    LODOP.PRINT();
                } else {
                    this.$confirm('Web打印服务CLodop未安装启动，请先安装！', '提示', {
                        confirmButtonText: '安装',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(()=> {
                        this.downloadExe()
                    })
      
                }
            })
        },
        downloadExe() {
            exportFile({
                downloadUrl: process.env.VUE_APP_PRINT_URL,
            })
        },
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            querySupplierProcurementInfo(qyparams)
                .then(res => {
                    this.tableData = res.data?.list ?? []
                    this.total = res.data?.total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
    },
}
</script>

<style></style>
